<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class Index extends Controller
{
    // 列表首页
	public function index()
    {
    	$newslist=Db::table('news')->select();
    	$this->assign('list',$newslist);
        return $this->fetch();
    }
    // 添加页面
    public function add()
    {
        return $this->fetch();
    }
    // 修改页面
    public function edit()
    {
        $id=input("id");
        $news=Db::table('news')->where('Id',$id)->find();
        $this->assign('news',$news);
        return $this->fetch();
    }
    // 删除
    public function del()
    {
        $id=input("id");
        if(Db::table('news')->where('Id',$id)->delete()){
            $this->success('删除成功', 'Index/index');
        }else{
            $this->error('删除失败');
        }

    }
    // 修改
    public function editNews()
    {
        $post=Request::instance()->post();
        $id=$post['id'];
        $data['title']=$post['title'];
        $data['content']=$post['content'];
        if(Db::table('news')->where('Id',$id)->update($data)){
            $this->success('修改成功', 'Index/index');
        }else{
            $this->error('修改失败');
        }
    }
    // 添加
    public function addNews()
    {
    	$post=Request::instance()->post();
    	$data['title']=$post['title'];
    	$data['content']=$post['content'];
    	if(Db::table('news')->insert($data)){
    		$this->success('新增成功', 'Index/index');
    	}else{
    		$this->error('新增失败');
    	}
    }
    // 跳转页面示例
    public function hello($name = 'thinkphp')
    {
        $this->assign('name', $name);
        return $this->fetch();
    }
}
